<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label class="layui-form-label">火化日期</label>
        <div class="layui-inline">
          <el-radio-group v-model="selectDataEixend.regDateValue" @change="getData">
            <el-radio-button :label="DateSelectMode.now" class="radio">当日</el-radio-button>
            <el-radio-button :label="DateSelectMode.all" class="radio">全部</el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">业务编号</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.RegBusnumEquals" placeholder="请输入业务编号" @keydown.enter.native="btnSearch"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">炉子编号</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.CrenameLike" placeholder="请选择火化炉编号" filterable @change="btnSearch">
            <el-option label="不选择" value=""></el-option>
            <el-option v-for="item in creNames" :key="item.value" :label="item.label" :value="item.label">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">炉子类型</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.RegcretypeEquals" placeholder="请选择火化炉类型" filterable @change="btnSearch">
            <el-option label="不选择" value=""></el-option>
            <el-option v-for="item in creTypeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">逝者姓名</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.RegnameLike" placeholder="请输入逝者姓名" @keydown.enter.native="btnSearch"></el-input>
        </div>
      </div>
       <div v-if="showstatus" class="layui-inline">
        <label class="layui-form-label">火化炉使用状态</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.CRHSTATUSLH" placeholder="请选择" style="width:100%" @change="btnSearch">
            <el-option v-for="item in crestatus" :key="item.Value" :label="item.Text" :value="item.Value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div v-if="hidden === 1" class="layui-inline">
        <label class="layui-form-label">火化工</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.Crhoprusr" placeholder="请选择火化炉编号" style="width:100%" @change="btnSearch">
            <el-option v-for="item in creUSES" :key="item.STAID" :label="item.STANAME" :value="item.STAID">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">开始日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.CrhbegdateSta" type="date" placeholder="请选择日期" @change="btnSearch">
          </el-date-picker>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">结束日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.CrhbegdateEnd" type="date" placeholder="请选择日期" @change="btnSearch">
          </el-date-picker>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>
    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <!--操作弹窗  -->
    <main-model ref="dialog" :isUse="isUse" @refreshData="getData"> </main-model>
        <!-- 火化证明pdf-->
        <sweet-modal ref="dialog1" class="dialog" :sweetModalMaxWidth="pageWidth">
            <h3>打印火化证明</h3>
            <iframe :src="pdfUrl" style="width:1150px; height:500px;margin-top:20px;"></iframe>
        </sweet-modal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-form-item {
  margin-bottom: 0px;
}
.btnCenter {
  text-align: center;
}
</style>
<script>
import mainModel from "./creUseMain";
import apiBase from "@/api/apiBase.js";
import dataTable from "@/components/Table/DataTable";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { ORDER } from "@/const/orderConst.js";
import { DATESELECTMODE } from "@/const/const.js";  // 上虞新加火化日期
 import sweetModal from "@/components/sweetModal";
const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
const BEG = "开始使用";
const END = "结束使用";
const PROVE = "打印火化证明";
export default {
  name: "creUse",
  mixins: [manage],
  data() {
    return {
      creNames: [],
      creTypeOptions: [],
      pageWidth: 1200,
      pdfUrl: null,
      DateSelectMode: DATESELECTMODE,   //上虞加火化日期
      selectDataEixend: {
        regDateValue: null,
        RegBusnumEquals: null,
      },
      selectData: {},
      // 火化工
      creUSES:{},
      crestatus:[],
      hidden: null,
      isUse: null,
      isywgetdata: null,//控制结束使用是否自动获取数据
      ischangered: null,
      ischangnb: null,
      showstatus: null, //火化炉使用状态
    };
  },
  methods: {
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case ADD:
          this.$refs.dialog.open(this.order);
          break;
        case BEG:
          if(this.isUse === 1){
            if(this.currentRow==null){
              this.$message({
                message: '请选择一条数据',
                type: 'warning'
              });
            }else{
               this.$refs.dialog.open(this.order, this.currentRow.CRHID);
            }
          }else{
            this.$refs.dialog.open(this.order);
          }
          break;
        case MODIFY:
        case INFO:
          this.$refs.dialog.open(this.order, this.currentRow.CRHID,this.currentRow.CRHSTATUS);
          break;
        case END:
          if(this.isywgetdata){
            this.$refs.dialog.open(this.order, this.currentRow.CRHID);
          }else{
            if (this.currentRow.CRHENDDATE != " ") {
              alert("该条记录已结束使用");
            } else {
              this.$refs.dialog.open(this.order, this.currentRow.CRHID);
            }
          }
         
          break;
        case DELETE:
          this.dataDelete();
          break;
        case ORDER.PROVE://打印火化证明
         this.getPdf(this.currentRow.REGID);
          break;
        default:
      }
    },
   
    btnShow(val){
      switch(val){
        case BEG:
        case ADD:
          return true;
          break;
        case MODIFY: 
         if(this.currentRow){
         if(this.currentRow.CREID!="")
         return true 
          }
         break;
        case DELETE:
        case INFO:
        case PROVE:
          if(this.currentRow) return true;
          break;
        case END:
          if(this.isywgetdata){
            return true
          }else{
            if(this.currentRow && this.currentRow.CRHBEGDATE != " ") return true;
          }
          break;
        default:
      }
    },
    //打印火化证明单
    getPdf(id) {
      this.pdfUrl = null;
      let datas = {
          REGID: id,
      };
      apiBase.DoService(
          datas,
          apiBase.getApiAdress().Pdf.UpdatePrintCre,
          res => {
              this.pdfUrl = res.data.content.PDFNAME;
              this.$refs.dialog1.open();
          },
          res => {
              this.$message.error(res.data.msg);
          }
      );
    
    },
    //获取表头
    getCreColumn() {
      apiBase.DoService(
        {},
        apiBase.getApiAdress().Column.GetCremationUseColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    //获取查询数据
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.CrhbegdateSta = formatDate(Data.CrhbegdateSta, "yyyyMMdd");
      Data.CrhbegdateEnd = formatDate(Data.CrhbegdateEnd, "yyyyMMdd");
      Data.RegcremationDateEquals = null;
      if(this.selectDataEixend.regDateValue == DATESELECTMODE.now){
        Data.RegcremationDateEquals = formatDate(new Date(), "yyyyMMdd");
      }
      var data = {
        PageIndex: this.pageOrder.currentPage,
        PageSize: this.pageOrder.pageSize,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().CremationUse.Query,
        res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
          this.dataList.forEach(data => {
            data.CRHBEGDATE = data.CRHBEGDATE.substring(0, 16);
            data.CRHENDDATE = data.CRHENDDATE.substring(0, 16);
            this.$nextTick(function() {
                let rows = document.getElementsByClassName("el-table__row");
                if(this.ischangered){
                  this.dataList.forEach((item,index) => {
                      rows[index].style.color = '#606266';
                      if(item.REGCRETYPE === 'A'){
                          rows[index].style.color = 'red';
                      }
                  });
                };
                if(this.ischangnb){
                  this.dataList.forEach((item,index) => {
                      rows[index].style.color = '#606266';
                      if(item.REGCRETYPE === 'A'||item.REGCRETYPE === 'C'){
                          rows[index].style.color = '#007eff';
                      }
                      if(item.REGCRETYPE === 'D'){
                          rows[index].style.color = 'red';
                      }
                  });
                };
            });
          });
        }
      );
    },
    //删除
    dataDelete() {
      var data = {
        CRHID: this.currentRow.CRHID
      };
      this.$confirm("此操作将永久删除该使用信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          apiBase.DoService(
            data,
            apiBase.getApiAdress().CremationUse.Delete,
            res => {
              this.getData();
              this.$message({
                message: "删除火化炉成功！",
                type: "success"
              });
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //重置查询
    btnRepeat() {
      this.selectDataEixend.regDateValue = DATESELECTMODE.now;
      this.selectData = {};
      this.getData();
    },
    initSeachdata() {
      this.selectDataEixend.regDateValue = DATESELECTMODE.now;
    },
  },

  components: {
    sweetModal,
    mainModel,
    dataTable
  },
  created() {
    let Data = JSON.parse(JSON.stringify({stacodnameEqual: "火化工"}));
    apiBase.DoService(
      {PageIndex: 1, PageSize: 10, OrderBy: null, OrderByType: null, Data: Data},
      apiBase.getApiAdress().Staff.Query,
      res => {
        this.creUSES = res.data.content.OutData
      });
    // 火化炉
    apiBase.DoService({},
      apiBase.getApiAdress().CremationUse.QueryCremation,
      res => {
        this.creNames = res.data.content.map(item => ({
          label: item.CRENO,
          value: item.CREID
        }));
      }
    );
    // 火化炉类型
    apiBase.DoService({},
      apiBase.getApiAdress().Code.GetCremationType,
      res => {
        this.creTypeOptions = res.data.content.map(item => ({
          label: item.CodeName,
          value: item.CodeValue
        }));
      }
    );
    apiBase.DoService(
      {},
      apiBase.getApiAdress().Code.QueryCRHSTATUS,
      res => {
          this.crestatus = res.data.content
      });
    apiBase.DoService(
      {},
      apiBase.getApiAdress().Param.GetCrePage,
      res => {
          this.hidden = res.data.content
      });
      // 个性化火化炉使用是否使用登记
    apiBase.DoService(
      {},
      apiBase.getApiAdress().Param.GetRegisterCreate,
      res => {
        this.isUse = res.data.content;
      });
      //控制结束使用是否自动获取数据
    apiBase.DoService(
      {},
      apiBase.getApiAdress().Param.GetIsControlUserQuery,
      res => {
        this.isywgetdata = res.data.content;
      });
    apiBase.DoService({},apiBase.getApiAdress().Param.GetIsDataTableReddening,res => {
        this.ischangered = res.data.content; 
    });
    apiBase.DoService({},apiBase.getApiAdress().Param.GetIsCreChangeRed,res => {
        this.ischangnb = res.data.content; 
    });
    //是否显示选择火化炉使用状态查询
    apiBase.DoService({},apiBase.getApiAdress().Param.GetIsCRHShowCreStatus,res => {
        this.showstatus = res.data.content; 
    });
    this.getCreColumn();
    this.initSeachdata();
    this.getData();
  },
  mounted() {
  },
  //路由的时候刷新组件
  activated() {
    this.getData();
  },
};
</script>